package com.css.fxfzypg.manage.lethalitydifference.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzypg.manage.lethalitydifference.vo.ExcelVo;
import com.css.fxfzypg.modules.surveytaskmanage.service.FieldSurveyTaskService;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Component
public class LethalityDifferenceExcelVerifyHandler  implements IExcelVerifyHandler<ExcelVo> {

    @Autowired
    FieldSurveyTaskService fieldSurveyTaskService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(ExcelVo excelVo) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");

        if(PlatformObjectUtils.isEmpty(excelVo.getTaskCode()) && PlatformObjectUtils.isEmpty(excelVo.getProvince()) && PlatformObjectUtils.isEmpty(excelVo.getCity()) && PlatformObjectUtils.isEmpty(excelVo.getArea())
                && PlatformObjectUtils.isEmpty(excelVo.getBuilding()) && PlatformObjectUtils.isEmpty(excelVo.getBuildingExplain())
                && PlatformObjectUtils.isEmpty(excelVo.getPeopleNum()) && PlatformObjectUtils.isEmpty(excelVo.getPeopleNumExplain())
                && PlatformObjectUtils.isEmpty(excelVo.getTerrain()) && PlatformObjectUtils.isEmpty(excelVo.getTerrainExplain())
                && PlatformObjectUtils.isEmpty(excelVo.getTraffic()) && PlatformObjectUtils.isEmpty(excelVo.getTrafficExplain())
                && PlatformObjectUtils.isEmpty(excelVo.getClimate()) && PlatformObjectUtils.isEmpty(excelVo.getClimateExplain())){
            result.setMsg(message.toString());
            return result;
        }

        if (PlatformObjectUtils.isEmpty(excelVo.getTaskCode())) {
            result.setSuccess(false);
            message.append("任务编号 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getProvince())) {
            result.setSuccess(false);
            message.append("省 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getCity())) {
            result.setSuccess(false);
            message.append("市 不能为空、");
        }

        if (PlatformObjectUtils.isEmpty(excelVo.getArea())) {
            result.setSuccess(false);
            message.append("区/县 不能为空、");
        }

        if (PlatformObjectUtils.isEmpty(excelVo.getBuilding())) {
            result.setSuccess(false);
            message.append("建筑物指标值 不能为空、");
        }else{
            String building = excelVo.getBuilding();
            Double d = new Double(building);
            if(d<0 || d>1){
                result.setSuccess(false);
                message.append("建筑物指标值 请输入[0,1]之间的数字、");
            }
        }

        if (PlatformObjectUtils.isEmpty(excelVo.getBuildingExplain())) {
            result.setSuccess(false);
            message.append("建筑物说明 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getPeopleNum())) {
            result.setSuccess(false);
            message.append("人口指标值 不能为空、");
        }else{
            Double d = new Double(excelVo.getPeopleNum());
            if(d<0 || d>1){
                result.setSuccess(false);
                message.append("人口指标值 请输入[0,1]之间的数字、");
            }
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getPeopleNumExplain())) {
            result.setSuccess(false);
            message.append("人口说明 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getTerrain())) {
            result.setSuccess(false);
            message.append("地形地貌指标值 不能为空、");
        }else{
            Double d = new Double(excelVo.getTerrain());
            if(d<0 || d>1){
                result.setSuccess(false);
                message.append("地形地貌指标值 请输入[0,1]之间的数字、");
            }
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getTerrainExplain())) {
            result.setSuccess(false);
            message.append("地形地貌指标值说明 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getTraffic())) {
            result.setSuccess(false);
            message.append("道路交通指标值 不能为空、");
        }else{
            Double d = new Double(excelVo.getTraffic());
            if(d<0 || d>1){
                result.setSuccess(false);
                message.append("道路交通指标值 请输入[0,1]之间的数字、");
            }
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getTrafficExplain())) {
            result.setSuccess(false);
            message.append("道路交通说明 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getClimate())) {
            result.setSuccess(false);
            message.append("气候指标值 不能为空、");
        }else{
            Double d = new Double(excelVo.getClimate());
            if(d<0 || d>1){
                result.setSuccess(false);
                message.append("气候指标值 请输入[0,1]之间的数字、");
            }
        }
        if (PlatformObjectUtils.isEmpty(excelVo.getClimateExplain())) {
            result.setSuccess(false);
            message.append("气候说明 不能为空、");
        }

        //校验任务编号是否存在
        List<String> allTaskNum = new ArrayList<>();
        List<Map<String, Object>> taskNum = fieldSurveyTaskService.getAllTaskNum();
        if (!PlatformObjectUtils.isEmpty(taskNum)) {
            for (Map<String, Object> map : taskNum) {
                String taskCode = map.get("task_num").toString();
                allTaskNum.add(taskCode);
            }
            if(!allTaskNum.contains(excelVo.getTaskCode())){
                result.setSuccess(false);
                message.append("任务编号不存在,请重新填写、");
            }
        }else{
            result.setSuccess(false);
            message.append("任务编号不存在,请重新填写、");
        }
        String mess = message.toString();
        if (!PlatformObjectUtils.isEmpty(mess)) {
            mess = mess.substring(0,mess.length()-1)+"。";
        }
        result.setMsg(mess);
        return result;
    }

    public static void main(String[] args) {
        boolean flat = Pattern.matches("(0.(\\d)+)|[0-1]", "0.876");
        System.err.println(flat);
    }



}
